package com.itfreer.data.general;

import com.itfreer.data.general.cache.DataCacheRegister;
import com.itfreer.data.general.utils.ConvertCaptionUtils;

import java.util.List;

/**
 * 定义提供主动的数据缓存数据表类
 */
public abstract class CachedTableBase extends TableBase {
	
	/**
	 * 将数据绑定到自定义数据类型上
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> List<T> getEntityList(Class<T> c, IQueryMap queryMap) {
		Object obj = DataCacheRegister
				.getCache(this, DataCacheRegister.EntityList, queryMap);
		if (obj == null) {
			List<T> row = super.getEntityList(c, queryMap);
			if (queryMap != null && queryMap.getAddCaptionField()) {
				ConvertCaptionUtils.convertCaptionField(this,
						queryMap.getSelectFields(), row);
			}
			DataCacheRegister.setCache(this, DataCacheRegister.EntityList, queryMap, row);
			return row;
		} else {
			return (List<T>) obj;
		}
	}

	/**
	 * 分页查询
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> List<T> pageSelect(Class<T> c, IQueryMap queryMap) {
		Object obj = DataCacheRegister.getCache(this, DataCacheRegister.PageSelect, queryMap);
		if (obj == null) {
			List<T> list = super.pageSelect(c, queryMap);
			if (queryMap != null && queryMap.getAddCaptionField()) {
				ConvertCaptionUtils.convertCaptionField(this,
						queryMap.getSelectFields(), list);
			}
			DataCacheRegister.setCache(this, DataCacheRegister.PageSelect, queryMap, list);
			return list;
		} else {
			return (List<T>) obj;
		}
	}

	/**
	 * 获取对象实现
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> T getEntity(Class<T> c, IQueryMap queryMap) {
		Object obj = DataCacheRegister.getCache(this, DataCacheRegister.Entity, queryMap);
		if (obj == null) {
			Object row = super.getExecuteScalar(queryMap);
			DataCacheRegister.setCache(this, DataCacheRegister.Entity, queryMap, row);
			return (T)row;
		} else {
			return (T)obj;
		}
	}
	
	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void save(RowBase entity) {
		super.save(entity);
		DataCacheRegister.clearCache(this, DataCacheRegister.Save, entity);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public <T extends RowBase> void save(List<T> entity) {
		super.save(entity);
		DataCacheRegister.clearCache(this, DataCacheRegister.Save, entity);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void insert(RowBase entity) {
		super.insert(entity);
		DataCacheRegister.clearCache(this, DataCacheRegister.Insert, entity);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public <T extends RowBase> void insert(List<T> entity) {
		super.insert(entity);
		DataCacheRegister.clearCache(this, DataCacheRegister.Insert, entity);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void update(RowBase entity) {
		super.update(entity);
		DataCacheRegister.clearCache(this, DataCacheRegister.Update, entity);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public <T extends RowBase> void update(List<T> entity) {
		super.update(entity);
		DataCacheRegister.clearCache(this, DataCacheRegister.Update, entity);
	}

	/**
	 * 更新数据
	 * 
	 * @param updateMap
	 */
	@Override
	public void update(IUpdateMap updateMap) {
		super.update(updateMap);
		DataCacheRegister.clearCache(this, DataCacheRegister.Update, updateMap);
	}

	/**
	 * 删除表记录
	 * 
	 * @param queryMap
	 */
	@Override
	public void delete(IQueryMap queryMap) {
		super.delete(queryMap);
		DataCacheRegister.clearCache(this, DataCacheRegister.Delete, queryMap);
	}

	/**
	 * 删除记录行
	 */
	@Override
	public void delete(String guid) {
		super.delete(guid);
		DataCacheRegister.clearCache(this, DataCacheRegister.Delete, guid);
	}
	
	/**
	 * 删除记录行
	 */
	@Override
	public void delete(List<String> guids) {
		super.delete(guids);
		DataCacheRegister.clearCache(this, DataCacheRegister.Delete, guids);
	}
}